package org.lex.ftp.ui;

import java.awt.BorderLayout;
import java.io.File;
import javax.swing.*;

import org.lex.explorer.Explorer;
import org.lex.ftp.FTP;
import org.lex.ftp.ui.actions.UploadQueueAction;
import org.lex.ftp.ui.actions.UploadTransferAction;
import org.lex.ftp.ui.explorer.FTPExplorer;
import org.lex.ftp.ui.queue.QueuePane;
import org.lex.ftp.util.prefs.Keys;
import org.lex.net.ftp.FTPURL;
import org.lex.net.log.Log;
import org.lex.net.log.ui.TextPaneLog;
import org.lex.swing.ContainerFactory;


public class MainContentPane extends JPanel {
	private final FTP app;
	// --------------------------------------
	private JSplitPane mainSplitPane = null;
	private JSplitPane topSplitPane = null;
	private JSplitPane bottomSplitPane = null;
	private QueuePane queuePane = null;
	private Explorer fileExplorer = null;
	private FTPExplorer ftpExplorer = null;
	private StatusPane mainStatusPane = null;
	private TextPaneLog loggerPane = null;

	public MainContentPane(FTP app) {
		super();
		this.app = app;
		this.initialize();
	}

	public void open(FTPURL url) {
		this.getFTPExplorer().open(url);
	}

	public FTPExplorer getFTPExplorer() {
		if (null == this.ftpExplorer)
			this.ftpExplorer = new FTPExplorer(app);
		return this.ftpExplorer;
	}

	public Log getGlobalLogger() {
		return this.getLoggerPane();
	}

	public Explorer getFileExplorer() {
		if (fileExplorer == null) {
			Splash.setStatus("Loading File System...");
			String dir = FTP.getContext().getResourceMap().getString(Keys.DOWNLOAD_PATH, ".");
			fileExplorer = new Explorer(new File(dir));
			// config file explorer key binding
			Action[] actions = new Action[] { new UploadQueueAction(app), new UploadTransferAction(app) };
			JPopupMenu pop = fileExplorer.getFileExplorer().getPopupMenu();
			JTable table = fileExplorer.getFileExplorer().getTable();
			InputMap im = table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
			ActionMap am = table.getActionMap();
			pop.insert(new JPopupMenu.Separator(), 0);
			for (int i = 0; i < actions.length; i++) {
				Action a = actions[i];
				KeyStroke ks = (KeyStroke) a.getValue(Action.ACCELERATOR_KEY);
				if (null == ks)
					continue;
				else {
					String name = (String) a.getValue(Action.NAME);
					am.put(name, a);
					im.put(ks, name);
					pop.insert(a, 0);
				}
			}
		}
		return fileExplorer;
	}

	public StatusPane getStatusPane() {
		if (mainStatusPane == null) {
			mainStatusPane = new StatusPane();
		}
		return mainStatusPane;
	}

	public QueuePane getQueuePane() {
		if (queuePane == null)
			queuePane = new QueuePane(app);
		return queuePane;
	}

	// =======================
	private void initialize() {
		this.setLayout(new BorderLayout());
		this.add(getMainSplitPane(), java.awt.BorderLayout.CENTER);
		this.add(getStatusPane(), java.awt.BorderLayout.SOUTH);
		this.add(new MainToolBar(app), BorderLayout.NORTH);
	}

	JSplitPane getMainSplitPane() {
		if (mainSplitPane == null) {
			mainSplitPane = ContainerFactory.createBorderlessSplitPane();
			mainSplitPane.setName(getClass().getName() + ".mainSplitPane");
			mainSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
			mainSplitPane.setDividerSize(5);
			mainSplitPane.setTopComponent(getTopSplitPane());
			mainSplitPane.setBottomComponent(getBottomSplitPane());
			mainSplitPane.setResizeWeight(0.75D);
		}
		return mainSplitPane;
	}

	public JSplitPane getTopSplitPane() {
		if (topSplitPane == null) {
			topSplitPane = ContainerFactory.createBorderlessSplitPane();
			topSplitPane.setName(getClass().getName() + ".topSplitPane");
			topSplitPane.setResizeWeight(0.5D);
			topSplitPane.setDividerSize(5);
			topSplitPane.setLeftComponent(this.getFileExplorer());
			topSplitPane.setRightComponent(this.getFTPExplorer());
		}
		return topSplitPane;
	}

	JSplitPane getBottomSplitPane() {
		if (bottomSplitPane == null) {
			bottomSplitPane = ContainerFactory.createBorderlessSplitPane();
			bottomSplitPane.setName(getClass().getName() + ".bottomSplitPane");
			bottomSplitPane.setDividerSize(5);
			bottomSplitPane.setLeftComponent(new JScrollPane(getQueuePane()));
			bottomSplitPane.setRightComponent(new JScrollPane(getLoggerPane()));
			bottomSplitPane.setResizeWeight(0.5D);
		}
		return bottomSplitPane;
	}

	private TextPaneLog getLoggerPane() {
		if (null == this.loggerPane) {
			this.loggerPane = new TextPaneLog();
			this.loggerPane.setInheritsPopupMenu(true);
		}
		return this.loggerPane;
	}
}
